package com.jy.service;

import com.jy.mapper.StudentMapper;
import com.jy.pojo.Master;
import com.jy.pojo.SetTime;
import com.jy.pojo.Student;
import com.jy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class StudentServiceImp implements StudentService{

    @Autowired
    private StudentMapper studentMapper;

    //get、set 方法
    public StudentMapper getStudentMapper() {
        return studentMapper;
    }

    public void setStudentMapper(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }


//    学生注册
    public boolean logon(Student student) {


        boolean result = true;



        if(student.getUsername()!=null) {
            for (Student student1 : studentMapper.signInAll()) {



                    if (student1.getUsername().equals(student.getUsername())) {
                        result = true;
                        break;
                    } else {
                        result = false;

                    }

            }

        }

        if(result==false)
        {
            studentMapper.logon(student);
        }

        return result;
    }

//    学生登录
    public String signUp(String username, String password) {

        boolean result = false;
        String str=null;
        for (Student student : studentMapper.signInAll()){



            if (username!=null){
                if (student.getUsername().equals(username)&&student.getPassword().equals(password)){
                    str=student.getStu_id();
                    result=true;
                    break;
                }else {
                    result=false;
                }
            }
        }




        if(result==false)
            return Result.resultStatus(201,"登录失败");

        else
            return Result.resultData2(200,str);
    }


//    管理员登录
    @Override
    public boolean signUp2(String username,String password) {

        boolean result = false;

        for (Master master : studentMapper.signInAll2()){



            if (username!=null){
                if (master.getUsername().equals(username)&&master.getPassword().equals(password)){
                    result=true;
                    break;
                }else {
                    result=false;
                }
            }
        }
        return result;

    }

    //    学生签到
    public String signIn(String stu_id) {


        Date date = new Date();
        System.out.println(SetTime.startTime);
        System.out.println(SetTime.endTime);
        System.out.println(date);
        if(date.getTime()<= SetTime.startTime.getTime())
        {

            studentMapper.sign(studentMapper.signIn(stu_id));

            return Result.resultStatus(200,"签到成功");
        }
        else if(SetTime.startTime.getTime()<=date.getTime()&&date.getTime()<=SetTime.endTime.getTime())
        {
            studentMapper.late(studentMapper.signIn(stu_id));


            return Result.resultStatus(201,"签到迟到");
        }
        else
        {
            return Result.resultStatus(202,"签到失败");
        }


    }


//     显示所有
    public List<Student> signInAll() {

        List<Student> list = new ArrayList<>();

        for(Student student : studentMapper.signInAll())
        {

            list.add(student);

        }

        System.out.println("xixixi");
        return list;
    }

//    显示已签到
    public List<Student> signInReady() {
        return studentMapper.signInReady();
    }
//    显示迟到
    @Override
    public List<Student> signInDelay() {
        return studentMapper.signInDelay();
    }




    //    存成功签到的同学
    public void sign(Student student) {



         studentMapper.sign(student);
    }

//    存入未签到同学
    @Override
    public void late(Student student) {
         studentMapper.late(student);
    }

    //    删除接口
    public void deleteStudentById(String stu_id) {

         studentMapper.deleteStudentById(stu_id);
    }

//    修改接口
    public void updateStudent(Student student) {
         studentMapper.updateStudent(student);
    }

    @Override
    public void deleteReady() {
         studentMapper.deleteReady();
    }

    @Override
    public void deleteLate() {
         studentMapper.deleteLate();
    }
}
